
import http, { baseHeaders, baseParams } from "@/utils/request";
import { UrlGetTransferInDetail, UrlExtractInviteIncome, UrlExtractRechargeIncome, UrlGetInviteIncomeData, UrlGetInviteIncomeList, UrlGetMyInviteInfo, UrlGetMyTransactions, UrlGetRechargeIncomeData, UrlGetUserAccounts, UrlGetUserWallet, UrlSetWalletName, UrlWithdrawCreate, UrlWithdrawGetBalance, UrlGetTransferInEarnList, UrlGetTransferInExtractList, UrlGetRechargeIncomeConfig, UrlGetTransferInEarn, UrlGetInviteFriendEarn } from "@/utils/apiUrl";
import { isEmptyString } from "@/utils/utils";
import { Toast } from "antd-mobile";

/**
 * 获取accounts信息
 * @returns thunk
 */
export const getUserAccounts = (mainCoinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getUserAccounts: " + UrlGetUserAccounts);

        //组装参数
        const params = baseParams();
        params.mainCoinType = mainCoinType || "";

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetUserAccounts, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取accounts信息-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取accounts信息-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取钱包信息
 * @returns thunk
 */
export const getUserWallet = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getUserWallet: " + UrlGetUserWallet);

        //组装参数
        const params = baseParams();
        params.coinType = coinType || "";

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetUserWallet, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取钱包信息-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取钱包信息-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}

/**
 * 获取用户邀请信息
 * @returns thunk
 */
export const getMyInviteInfo = (reqCallback) => {
    return async (dispatch) => {
        console.log("getMyInviteInfo: " + UrlGetMyInviteInfo);

        //组装参数
        const params = baseParams();

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetMyInviteInfo, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取用户邀请信息-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取用户邀请信息-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}

/**
 * 设置用户钱包名称
 * @returns thunk
 */
export const setWalletName = (name, walletId, reqCallback) => {
    return async (dispatch) => {
        console.log("setWalletName: " + UrlSetWalletName);

        //组装参数
        const params = baseParams();
        params.name = name;
        params.walletId = walletId;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlSetWalletName, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("设置用户钱包名称-结束：=======" + resCode)
        console.log(resObj)
        console.log("设置用户钱包名称-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取余额
 * @returns thunk
 */
export const withdrawGetBalance = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("withdrawGetBalance: " + UrlWithdrawGetBalance);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlWithdrawGetBalance, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取余额-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取余额-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 发起提币
 * @returns thunk
 */
export const withdrawCreate = (coinType, address, amount, reqCallback) => {
    return async (dispatch) => {
        console.log("withdrawCreate: " + UrlWithdrawCreate);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;
        params.address = address;
        params.amount = amount;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlWithdrawCreate, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("发起提币-结束：=======" + resCode)
        console.log(resObj)
        console.log("发起提币-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取用户交易记录信息
 * @returns thunk
 */
export const getMyTransactions = (inOrOut, coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getMyTransactions: " + UrlGetMyTransactions);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;
        params.inOrOut = inOrOut;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetMyTransactions, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取用户交易记录信息-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取用户交易记录信息-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取充值收益数据
 * @returns thunk
 */
export const getRechargeIncomeData = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getRechargeIncomeData: " + UrlGetRechargeIncomeData);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetRechargeIncomeData, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取充值收益数据-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取充值收益数据-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 提取充值收益
 * @returns thunk
 */
export const extractRechargeIncome = (coinType, password, amount, reqCallback) => {
    return async (dispatch) => {
        console.log("extractRechargeIncome: " + UrlExtractRechargeIncome);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;
        params.password = password;
        params.amount = amount;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlExtractRechargeIncome, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("提取充值收益-结束：=======" + resCode)
        console.log(resObj)
        console.log("提取充值收益-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取邀请好友收益数据
 * @returns thunk
 */
export const getInviteIncomeData = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getInviteIncomeData: " + UrlGetInviteIncomeData);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetInviteIncomeData, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取邀请好友收益数据-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取邀请好友收益数据-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 提取邀请好友收益
 * @returns thunk
 */
export const extractInviteIncome = (coinType, password, amount, reqCallback) => {
    return async (dispatch) => {
        console.log("extractInviteIncome: " + UrlExtractInviteIncome);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;
        params.password = password;
        params.amount = amount;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlExtractInviteIncome, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("提取邀请好友收益-结束：=======" + resCode)
        console.log(resObj)
        console.log("提取邀请好友收益-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取邀请好友收益列表
 * @returns thunk
 */
export const getInviteIncomeList = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getInviteIncomeList: " + UrlGetInviteIncomeList);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetInviteIncomeList, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取邀请好友收益列表-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取邀请好友收益列表-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取转入收益详情
 * @returns thunk
 */
export const getTransferInDetail = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getTransferInDetail: " + UrlGetTransferInDetail);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetTransferInDetail, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取转入收益详情-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取转入收益详情-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取转入收益列表
 * @returns thunk
 */
export const getTransferInEarnList = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getTransferInEarnList: " + UrlGetTransferInEarnList);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetTransferInEarnList, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取转入收益列表-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取转入收益列表-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}

/**
 * 获取收益转出列表
 * @returns thunk
 */
export const getTransferInExtractList = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getTransferInExtractList: " + UrlGetTransferInExtractList);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetTransferInExtractList, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取收益转出列表-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取收益转出列表-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取充值入账收益率配置
 * @returns thunk
 */
export const getRechargeIncomeConfig = (reqCallback) => {
    return async (dispatch) => {
        console.log("getRechargeIncomeConfig: " + UrlGetRechargeIncomeConfig);

        //组装参数
        const params = baseParams();

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetRechargeIncomeConfig, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取充值入账收益率配置-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取充值入账收益率配置-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}

/**
 * 获取转入收益总览
 * @returns thunk
 */
export const getTransferInEarn = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getTransferInEarn: " + UrlGetTransferInEarn);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetTransferInEarn, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取转入收益总览-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取转入收益总览-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}


/**
 * 获取邀请好友收益总览
 * @returns thunk
 */
export const getInviteFriendEarn = (coinType, reqCallback) => {
    return async (dispatch) => {
        console.log("getInviteFriendEarn: " + UrlGetInviteFriendEarn);

        //组装参数
        const params = baseParams();
        params.coinType = coinType;

        const headers = baseHeaders();
        console.log(headers)

        //发起请求
        const res = await http.post(UrlGetInviteFriendEarn, params, headers)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取邀请好友收益总览-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取邀请好友收益总览-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        let isNeedToReLogin = (resCode === 401)
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
                if (res.msg.indexOf("token") !== -1) {
                    isNeedToReLogin = true
                }
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj, isNeedToReLogin)
    }
}